package com.kaibai.dao.impl;

import com.kaibai.dao.BaseDao;
import com.kaibai.dao.UserDao;
import com.kaibai.pojo.Good;
import com.kaibai.pojo.User;

import java.sql.Connection;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public int insert(Connection conn, User user) {
        String sql = "insert into user(name,age,email,sex,password) values(?,?,?,?,?)";
        return update(conn,sql,user.getName(),user.getAge(),user.getEmail(),user.getSex(),user.getPassword());
    }

    @Override
    public int deleteById(Connection conn, int id) {
        String sql = "delete from user where id=?";
        return update(conn,sql,id);
    }

    @Override
    public int update(Connection conn, User user) {
        String sql = "update user set name=? , age=? , email=? , sex=? , password=? where id=?";
        return update(conn,sql,user.getName(),user.getAge(),user.getEmail(),user.getSex(),user.getPassword(),user.getId());
    }

    @Override
    public User getUserById(Connection conn, int id) {
        String sql = "select id,name,age,email,sex,password from user where id=?";
        return getInstance(conn,User.class,sql,id);
    }

    @Override
    public List<User> getAll(Connection conn) {
        String sql = "select id,name,age,email,sex,password from user";
        return getForList(conn,User.class,sql);
    }

    @Override
    public User login(Connection conn, String name, String password) {
        String sql = "select id,name,age,email,sex,password from user where name=? and password=?";
        return getInstance(conn,User.class,sql,name,password);
    }

    @Override
    public User existsUsername(Connection conn, String name) {
        String sql = "select id,name,age,email,sex,password from user where name=?";
        return getInstance(conn,User.class,sql,name);
    }

    @Override
    public User getUserByName(Connection conn, String name) {
        String sql = "select id,name,age,email,sex,password from user where name=?";
        return getInstance(conn,User.class,sql,name);
    }

    @Override
    public Integer getForPageTotalCount(Connection conn) {
        String sql = "select count(*) from user";
        Number number =  getValue(conn,sql);
        return number.intValue();
    }

    @Override
    public List<User> getForPageItems(Connection conn, int begin, int pageSize) {
        String sql = "select id,name,age,email,sex,password from user limit ?,?";
        return getForList(conn, User.class,sql,begin,pageSize);
    }
}
